package testing;

import static org.junit.Assert.*;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import model.MP3File;

import org.junit.Before;
import org.junit.Test;

import controller.ID3Parser;
import controller.ID3Writer;
import controller.InvalidID3TagException;

/**
 * testcase for the ID3Writer
 * 
 *
 */
public class WriterTest {
	
	MP3File testFile = null;
	File newFile;
	
	@Before
	public void ini() {
		File someFile = Paths.get("./bin/testing/01Turtle.mp3").toFile();
		System.out.println(someFile.getAbsolutePath());
		System.out.println(someFile.isFile());
		
		newFile = new File("./bin/testing/test.mp3");
		try {
			Files.copy(someFile.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	
	@Test
	public void changeTitleTest() throws IOException, InvalidID3TagException {
				
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
			
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TIT2", "kack");
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			assertEquals("Wrong title", "kack", testFile.getTitle());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
		
	}
	
	@Test
	public void changeAlbumTest() throws IOException, InvalidID3TagException {
				
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
			
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TALB", "muh");
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			assertEquals("Wrong album", "muh", testFile.getAlbum());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
		
	}
	
	
	@Test
	public void changeYearTest() throws IOException, InvalidID3TagException {
				
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
			
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TYER", "1500");
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			assertEquals("Wrong year", 1500, testFile.getYear());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
		
	}
	@Test
	public void changeInterpretTest() throws IOException, InvalidID3TagException {
				
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
			
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TPE1", "muh");
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			assertEquals("Wrong interpreter", "muh", testFile.getArtist());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
		
	}
	
	@Test
	public void changeCover() throws IOException, InvalidID3TagException {
		
		BufferedImage image = null;
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
			
			image = ImageIO.read(new File("./bin/testing/nocover.jpg"));
			System.out.println("bild eingelesen");
			
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		try {
			ImageIcon icon = new ImageIcon(image);
			int height = icon.getIconHeight();
			int width = icon.getIconWidth();

			BufferedImage im = new BufferedImage(width, height,BufferedImage.TYPE_4BYTE_ABGR);
			im.getGraphics().drawImage(image, 0,0, icon.getImageObserver());
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
		 	try {
			 	ImageIO.write(im, "png", stream);
			}catch (IOException e){
				e.printStackTrace();
		 	}
			byte[] imageData = stream.toByteArray();
			System.out.println("bild in byte umgewandelt");
			try {
				stream.close();
			} catch (IOException e) {
				System.out.println("outputstream konnte nicht geschlossen werden");
				e.printStackTrace();
			}
			ID3Writer.changeContentMP3FrameWithKey(testFile, "APIC", imageData);
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
		
	}
	
	@Test
	public void changeAllTest() throws IOException, InvalidID3TagException {
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TPE1", "muh");
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TYER", "1500");
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TALB", "muh");
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TIT2", "muh");
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			
			assertEquals("Wrong interpreter", "muh", testFile.getArtist());
			assertEquals("Wrong year", 1500, testFile.getYear());
			assertEquals("Wrong album", "muh", testFile.getAlbum());
			assertEquals("Wrong title", "muh", testFile.getTitle());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
	} 

}
